//
//  KeepaliveManager.m
//  999-考勤定位
//
//  Created by 李晓东 on 2018/4/12.
//  Copyright © 2018年 PeanutXu. All rights reserved.
//

#import "KeepaliveManager.h"

@implementation KeepaliveManager{
    NSMutableArray* _bgTaskIdList; //后台任务数组
    UIBackgroundTaskIdentifier _masterTaskId; //当前后台任务id
    NSString *_sayHi;
    
}
UIBackgroundTaskIdentifier *demoTask;
static UIApplication *application;
//static UIBackgroundTaskIdentifier backTaskId;

+ (instancetype)shareManager {
    static dispatch_once_t onceToken;
    static KeepaliveManager *manager;
    dispatch_once(&onceToken, ^{
        manager = [[KeepaliveManager alloc]init];
        application = [UIApplication sharedApplication];
        
    });
    return manager;
}

-(instancetype)init
{
    if (self == [super init]) {
        _bgTaskIdList = [NSMutableArray array];
        _masterTaskId = UIBackgroundTaskInvalid;
        demoTask = UIBackgroundTaskInvalid;
    }
    return self;
}

//- (void)callBackgroundTask {
//    UIApplication *application = [UIApplication sharedApplication];
//    __block UIBackgroundTaskIdentifier bgTaskId = UIBackgroundTaskInvalid;
//    if([application respondsToSelector:@selector(beginBackgroundTaskWithExpirationHandler:)])
//    {
//        bgTaskId = [application beginBackgroundTaskWithExpirationHandler:^{
//            NSLog(@"bgTask 过期 %lu",(unsigned long)bgTaskId);
//            bgTaskId = UIBackgroundTaskInvalid;
//            [application endBackgroundTask:bgTaskId];
//        }];
//    }
//
//    if (_masterTaskId == UIBackgroundTaskInvalid) {
//        _masterTaskId = bgTaskId;
//        NSLog(@"开启后台任务 %lu",(unsigned long)bgTaskId);
//    } else {
//        [_bgTaskIdList addObject:@(bgTaskId)];
//        [self endBackGroundTask:NO];
//    }
//
//}
// 我的理解是.这里用到的 backTaskId 每次endBackgroundTask 时,都是取消 backTaskId 对应的地址所的值.那么以后再对该地址赋值,他都不能再beginBackgroundTask了
// 而下面用到的把 backTaskId 保存到数组中.每次取消时,再从数组中取出并赋给另一个地址.每次取消用到的是该新的地址.所以用到的 backTaskId 还能再次使用
- (void)callBackgroundTask {
    int hi = arc4random_uniform(30);
    _sayHi = [NSString stringWithFormat:@"SayHi%d", hi];

    __block UIBackgroundTaskIdentifier backTaskId = UIBackgroundTaskInvalid;
    backTaskId = [application beginBackgroundTaskWithExpirationHandler:^{
        backTaskId = UIBackgroundTaskInvalid;
        [application endBackgroundTask:backTaskId];

    }];
    if (demoTask != NULL) {
        if (*demoTask != backTaskId) {
            [application endBackgroundTask:*demoTask];
            
        }
    }

    demoTask = &backTaskId;

}


//-(void)endBackGroundTask:(BOOL)all
//{
//    UIApplication *application = [UIApplication sharedApplication];
//    //如果为all 清空后台任务数组
//    //不为all 留下数组最后一个后台任务,也就是最新开启的任务
//    if ([application respondsToSelector:@selector(endBackGroundTask:)]) {
//        for (int i = 0; i < (all ? _bgTaskIdList.count :_bgTaskIdList.count -1); i++) {
//            UIBackgroundTaskIdentifier bgTaskId = [_bgTaskIdList[0]integerValue];
//            NSLog(@"关闭后台任务 %lu",(unsigned long)bgTaskId);
//            [application endBackgroundTask:bgTaskId];
//            [_bgTaskIdList removeObjectAtIndex:0];
//        }
//    }
//    ///如果数组大于0 所有剩下最后一个后台任务正在跑
//    if(_bgTaskIdList.count > 0)
//    {
//        NSLog(@"后台任务正在保持运行 %ld",(long)[_bgTaskIdList[0]integerValue]);
//    }
//
//}
@end
